<?php
namespace Manage\Model;
use Think\Model;
class ProductModel extends Model{
	public function operProduct($data,$product_id=0){
		$product_data=$this->create($data);
		$product_data['date_modified']=time();
		if($product_id){		
			$this->where(array('product_id'=>$product_id))->save($product_data);
		}else{
			$product_data['date_added']=time();
			$product_id=$this->add($product_data);
		}
		$ProductDescriptionModel=M('ProductDescription');
		$map['product_id']=$product_id;
		$ProductDescriptionModel->where($map)->delete();
		foreach ($data['product_description'] as $language_id => $product_description) {
			$product_description['language_id']=$language_id;
			$product_description['product_id']=$product_id;
			$ProductDescriptionModel->add($product_description);
		}
		//图片
		$ProductImageModel=M('ProductImage');
		$map['product_id']=$product_id;
		$ProductImageModel->where($map)->delete();
		foreach ($data['product_image'] as $image) {
			$image['product_id']=$product_id;
			$image['text']=json_encode($image['text']);
			$ProductImageModel->data($image)->add();

		}
		//属性
		$ProductAttributeModel=M('ProductAttribute');
		$map=array('product_id'=>$product_id);
		$ProductAttributeModel->where($map)->delete();
		$attribute['product_id']=$product_id;
		foreach ($data['product_attribute'] as $product_attribute) {
			foreach ($product_attribute['product_attribute_description'] as $language_id => $description) {
				$attribute['attribute_id']	=	$product_attribute['attribute_id'];
				$attribute['language_id']	=	$language_id;
				$attribute['text']			=	$description['text'];
				$ProductAttributeModel->add($attribute);
			}

		}
		//相关
		$ProductRelated=M('ProductRelated');
		$ProductRelated->where($map)->delete();
		$ProductRelated->where(array('related_id'=>$product_id))->delete();
		foreach ($data['product_related'] as $related_id) {
			$related=array(
				'product_id'	=>	$product_id,
				'related_id'	=>	$related_id
				);
			$ProductRelated->add($related);
			$related=array('product_id'=>$related_id,'related_id'=>$product_id);
			$ProductRelated->add($related);
		}
//		//下载
//		$ProductToDownload=M('ProductToDownload');
//		$ProductToDownload->where($map)->delete();
//		foreach($data['product_download'] as $download_id){
//			$download=array(
//				'product_id'	=>	(int)$product_id,
//				'download_id'	=>	(int)$download_id
//				);
//			$ProductToDownload->add($download);
//		}

	}
	public function getProduct($product_id=0){
		$map=array('product_id'=>(int)$product_id);
		$product=$this->where($map)->find();
		$pd=M('ProductDescription')->where($map)->select();
		foreach ($pd as $v) {
			$product['product_description'][$v['language_id']]=$v;
		}
		$product['product_image']=$this->getProductImage($product_id);
		$product['product_attributes']=$this->getProductAttributes($product_id);
		$product_related=M('ProductRelated')->where($map)->select();
		foreach ($product_related as $related) {
			$product['product_related'][]=$related['related_id'];
		}
//		$product_download=M('ProductToDownload')->where($map)->select();
//		foreach ($product_download as $download){
//			$product['product_download'][]=$download['download_id'];
//		}
		if(empty($product['product_image'])) unset($product['product_image']);
		if(empty($product['product_attributes'])) unset($product['product_attributes']);
		return $product;
	}
	//商品图片
	public function getProductImage($product_id){
		$images=M('ProductImage')->where(array('product_id'=>$product_id))->order('sort_order ASC')->select();
		foreach ($images as $k => $v) {
			$images[$k]['text']=json_decode($v['text'],true);
		}
		return $images;
	}
	//商品属性
	public function getProductAttributes($product_id=0){
		$map['product_id']=$product_id;
		$ProAttModel=M('ProductAttribute');
		$AttrDesc=M('AttributeDescription');
		$admap['language_id']=get_default_lng_id();
		$return =array();
		$product_attributes=$ProAttModel->where($map)->field('attribute_id')->group('attribute_id')->select();
		foreach ($product_attributes as $product_attribute) {
			$pro_attr_des_data=array();
			$map['attribute_id']	=	$product_attribute['attribute_id'];
			$admap['attribute_id']	=	$product_attribute['attribute_id'];
			$attribute_info = $AttrDesc->where($admap)->find();
			$pro_attr_des=$ProAttModel->where($map)->select();
			foreach ($pro_attr_des as $pad) {
				$pro_attr_des_data[$pad['language_id']]=array('text'=>$pad['text']);
			}
			$return[]=array(
				'attribute_id'		=>	$product_attribute['attribute_id'],
				'name'				=>	$attribute_info['name'],
				'product_attribute_description'	=>	$pro_attr_des_data
				);
		}
		return $return;
	}
	public function getProducts($data){
		$page=array(
			'start'		=>	$data['start']?$data['start']:0,
			'limit'		=>	$data['limit']?$data['limit']:20
			);
		$map=array(
			'language_id'	=>	get_default_lng_id()
			);
		$order='sort_order '. (isset($data['order'])?$data['order']:'ASC');
		$order .= ',product_id DESC';
		if(isset($data['status'])) $map['status']=$data['status'];
		if(isset($data['category_id'])) {
			$ids=D('Category')->getChildId($data['category_id']);
			$map['category_id']=array('IN',$ids);
		}
		if(isset($data['title'])) $map['title'] = array('like',"%".$data['title']."%");

		$products=$this->join('LEFT JOIN  __PRODUCT_DESCRIPTION__ ON __PRODUCT__.product_id=__PRODUCT_DESCRIPTION__.product_id')->where($map)->field(C('DB_PREFIX')."product.product_id,title,sort_order,category_id,image,status")->limit($page['start'],$page['limit'])->order($order)->select();
		return $products;
	}
	public function getCount($data){
		$map=array(
			'language_id'	=>	get_default_lng_id()
			);
		if(isset($data['status'])) $map['status']=$data['status'];
		if(isset($data['category_id'])) {
			$ids=D('Category')->getChildId($data['category_id']);
			$map['category_id']=array('IN',$ids);
		}
		if(isset($data['title'])) $map['title'] = array('like',"%".$data['title']."%");

		$count=$this->join('LEFT JOIN  __PRODUCT_DESCRIPTION__ ON __PRODUCT__.product_id=__PRODUCT_DESCRIPTION__.product_id')->where($map)->field(C('DB_PREFIX')."product.product_id,title,category_id,image,status")->count();
		return $count;
	}
	public function del($product_id){
		$map['product_id']=(int)$product_id;
		//删除描述
		$ProductDescription=M('ProductDescription');
		$ProductDescription->where($map)->delete();
		//删除图片
		$ProductImage=M('ProductImage');
		$ProductImage->where($map)->delete();
		//删除属性
		M('ProductAttribute')->where($map)->delete();
		//删除关联
		$RelatedModel=M('ProductRelated');
		$RelatedModel->where($map)->delete();
		$RelatedModel->where(array('related_id'=>$product_id))->delete();
//		//删除下载(附件)
//		M('ProductToDownload')->where($map)->delete();
		return $this->where($map)->delete();
	}
}